package com.fjnldx.mes.service.scheduling.alarm.rule.relieve;

import com.fjnldx.mes.domain.pojo.alarm.AlarmRecord;
import com.fjnldx.mes.domain.pojo.alarm.AlarmRules;
import com.fjnldx.mes.domain.pojo.alarm.enums.RelieveTypeEnum;
import com.fjnldx.mes.service.scheduling.alarm.model.MonitorModel;
import com.fjnldx.mes.service.scheduling.alarm.rule.inspector.AlarmRuleCheck;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 恢复到规则检查
 */
@Service
public class RestoreCheck implements AlarmRelieveCheck {

    private final List<AlarmRuleCheck> alarmRuleCheckList;

    RestoreCheck(List<AlarmRuleCheck> alarmRuleChecks) {

        alarmRuleCheckList = alarmRuleChecks;
    }

    @Override
    public RelieveTypeEnum RelieveType() {
        return RelieveTypeEnum.RESTORE;
    }

    @Override
    public Boolean Check(AlarmRules rule, AlarmRecord record) throws Exception {
        try {
            List<AlarmRuleCheck> itemCheckServices = alarmRuleCheckList.stream().filter(service -> service.monitoringType() == rule.getMonitorType()).collect(Collectors.toList());
            return itemCheckServices.stream().allMatch(checkService -> {
                try {
                    MonitorModel check = checkService.Check(rule);
                    return check != null && !check.getWhetherTrigger();
                } catch (Exception e) {
                    return false;
                }
            });
        } catch (Exception e) {
            throw new Exception();
        }
    }
}
